package mess;

import java.util.*;

public class LeeCode_face_17_05_02 {
    public static void main(String[] args) {
        String[] res = findLongestSubarray(new String[]{
                "A","B","1","2","C"});
        System.out.println(Arrays.toString(res));
       /* String[] res2 = findLongestSubarray(new String[]{
                "A","1","B","C","D","2","3","4","E","5","F","G","6","7","H","I","J","K","L","M"});
        System.out.println(Arrays.toString(res2));*/

    }
    private static String[] findLongestSubarray(String[] array) {
        Map<Integer, Integer> vix = new HashMap<>();
        vix.put(0, -1);
        int mx = 0;
        int k = 0;
        int s = 0;
        for (int i = 0; i < array.length; i++) {
            s += array[i].charAt(0) >= 'A' ? -1 : 1;
            if (vix.containsKey(s)){
                int j = vix.get(s);
                if (i - j > mx){
                    mx = i - j;
                    k = j + 1;
                }
            }else {
                vix.put(s, i);
            }
        }
        return Arrays.copyOfRange(array, k, k + mx);
    }
}
